<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <input type="file" hidden ref="file" @change="onFilechange" />

    <!-- /导航栏 -->
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isShowUpdateName = true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isShowUpdateGender = true"
    />
    <van-cell
      @click="isShowUpdateBirthday = true"
      title="生日"
      :value="user.birthday"
      is-link
    />

    <!-- 名字 -->
    <van-popup
      v-model="isShowUpdateName"
      position="bottom"
      style="height: 100%"
    >
      <UpdateName
        v-if="isShowUpdateName"
        @close="isShowUpdateName = false"
        v-model="user.name"
      />
    </van-popup>

    <!-- 名字 -->

    <!-- 性别 -->
    <van-popup
      v-model="isShowUpdateGender"
      position="bottom"
      style="height: 100%"
    >
      <update-gender
        v-model="user.gender"
        @close="isShowUpdateGender = false"
      />
    </van-popup>

    <!-- 性别 -->

    <!-- 生日 -->
    <van-popup
      v-model="isShowUpdateBirthday"
      position="bottom"
      style="height: 100%"
    >
      <update-birthday
        v-if="isShowUpdateBirthday"
        v-model="user.birthday"
        @close="isShowUpdateBirthday = false"
      />
    </van-popup>

    <!-- 生日 -->

    <!-- 头像 -->
    <van-popup
      v-model="isShowUpdatePhoto"
      position="bottom"
      style="height: 100%"
    >
      <update-photo :img="img" />
    </van-popup>

    <!-- 头像 -->
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserPfofile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data() {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthday: false,
      isShowUpdatePhoto: false,
      img: null
    }
  },
  created() {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        console.log(data)
        this.user = data.data
      } catch (error) {
        this.$toast('数据请求失败')
      }
    },
    onFilechange() {
      const file = this.$refs.file.files[0]
      this.img = window.URL.createObjectURL(file)
      this.isShowUpdatePhoto = true
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
</style>
